<script setup>
import FirstTitle from "@comp/FirstTitle";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onMounted } from "vue";
import { postTableData } from "@/api/table";
import { searchDataRyfh } from "@/api/search";
import { addOrUpdateRyfh } from "@/api/addOrUpdate";

const city = window.globalObj.name;

const editorRef10 = shallowRef();
const valueHtml10 = ref(`接入单位共有XX个。具体情况如下：`);
const toolbarConfig10 = {};
const editorConfig10 = { placeholder: "请输入内容..." };
const mode10 = ref("default");
const handleCreated10 = (editor) => {
  editorRef10.value = editor; // 记录 editor 实例，重要！
};
const editorRef11 = shallowRef();
const valueHtml11 = ref(
  `共有XX个警报器，城市覆盖率XX%，完好率XX%。具体情况如下：`
);
const toolbarConfig11 = {};
const editorConfig11 = { placeholder: "请输入内容..." };
const mode11 = ref("default");
const handleCreated11 = (editor) => {
  editorRef11.value = editor; // 记录 editor 实例，重要！
};

// 表头单元格样式
const headerCellStyle = () => {
  return {
    backgroundColor: "#f5f7fa",
    color: "rgba(0,0,0,0.85)",
    fontSize: "14px",
    height: "54px",
  };
};
// 单元格样式
const cellStyle = () => {
  return {
    color: "rgba(0,0,0,0.65)",
    fontSize: "14px",
    lineHeight: "22px",
  };
};

// 空情预警信息系统情况统计表
const kqyjList = ref([]);
const getKqyjData = () => {
  return postTableData({
    docType: "protectioaction",
    method: "kongqingyujingxinxiqingkuang",
  }).then((res) => {
    if (res.code === 200) {
      kqyjList.value = res?.data || [];
    }
  });
};

// 警报系统情况统计表
const jbxtList = ref([]);
const getJbXTData = () => {
  return postTableData({
    docType: "protectioaction",
    method: "jingbaoxitongqingkuangtongji",
  }).then((res) => {
    if (res.code === 200) {
      jbxtList.value = res?.data || [];
    }
  });
};

// 加载html
const initHtml = () => {
  searchDataRyfh({ m: "任务需求预警报知能力现状" }).then((res) => {
    const { data } = res;
    valueHtml10.value = data.find((x) => x.k === "空情预警系统现状")?.v || "";
    valueHtml11.value = data.find((x) => x.k === "警报器现状")?.v || "";
  });
};
const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdateRyfh({
      m: "任务需求预警报知能力现状",
      data: [
        {
          k: "空情预警系统现状",
          v: valueHtml10.value,
        },
        {
          k: "警报器现状",
          v: valueHtml11.value,
        },
      ],
    })
      .then(() => {
        ElMessage.success("操作成功");
      })
      .catch(() => {});
  });
};

onMounted(async () => {
  initHtml();
  await getKqyjData();
  await getJbXTData();
});

onBeforeUnmount(() => {
  if (editorRef10.value) editorRef10.value.destroy();
  if (editorRef11.value) editorRef11.value.destroy();
});
</script>
<template>
  <div class="">
    <FirstTitle name="空情预警系统现状" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef10"
        :defaultConfig="toolbarConfig10"
        :mode="mode10"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml10"
        :defaultConfig="editorConfig10"
        :mode="mode10"
        @onCreated="handleCreated10"
      />
    </div>
    <div class="text-center top-15">{{ city }}空情预警信息系统情况统计表</div>
    <el-table
      id="table"
      :data="kqyjList"
      class="top-15"
      style="width: 100%"
      :header-cell-style="headerCellStyle"
      :cell-style="cellStyle"
      border
    >
      <el-table-column prop="rowNum" label="序号" width="100" align="center" />
      <el-table-column prop="jd_name" label="行政区划" />
      <el-table-column prop="kqtgdwmc" label="单位名称" />
      <el-table-column prop="kqjsdwdz" label="地址" />
      <el-table-column prop="zuobiao" label="坐标" />
    </el-table>

    <FirstTitle name="警报器现状" class="top-15" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef11"
        :defaultConfig="toolbarConfig11"
        :mode="mode11"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml11"
        :defaultConfig="editorConfig11"
        :mode="mode11"
        @onCreated="handleCreated11"
      />
    </div>
    <div class="text-center top-15">{{ city }}警报系统情况统计表</div>
    <el-table
      id="table"
      :data="jbxtList"
      class="top-15"
      style="width: 100%"
      :header-cell-style="headerCellStyle"
      :cell-style="cellStyle"
      border
    >
      <el-table-column prop="rowNum" label="序号" width="100" align="center" />
      <el-table-column prop="jd_name" label="单位" />
      <el-table-column prop="wz_area" label="面积（平方公里）" />
      <el-table-column prop="quantity" label="现有数量" />
      <el-table-column prop="cityCoverageRate" label="警报器城市覆盖率（%）" />
      <el-table-column prop="serviceability_rate" label="警报器完好率（%）" />
    </el-table>
    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss"></style>